package fivm.common.client.sys;

import com.fi.common.file.FileTool;
import com.fi.common.log.FiLog;

import fivm.cross.platform.steamobj.app.AppConfigs;
import fivm.cross.platform.steamobj.sys.DeviceConfigs;
import fivm.cross.platform.steamobj.sys.DeviceControlStream;


/**
 * 本来是可以直接用Config来操作整个控制的。
 * 但考虑框架一直是想将数据与操作分开。让数据便于交互
 * 所以在数据处理之上加上一层操作类。既就是此类
 * */

public class DeviceConfigClient  {
	private static final String TAG="DeviceConfig";
	private static boolean gTest=false;
	public static String ServerConfigurationPath="/data/local/tmp/vmtools/control_server.cfg";
	public static String DeviceControlStreamNativePath="/data/local/tmp/vmtools/sys/DeviceBusinuess.json";
	
	
	private DeviceConfigs deviceConfig;
	public DeviceConfigClient(DeviceConfigs deviceConfig){
		this.deviceConfig=deviceConfig;
	}
	
	
	public DeviceConfigs getConfig(){
		return deviceConfig;
	}
	public void setConfig(DeviceConfigs config){
		deviceConfig=config;
	}
	
	/**
	 * 判断参数进来是否正确的
	 * */
	public boolean isValid(){
		if(deviceConfig.getControlServerAddr()==null || deviceConfig.getControlServerPort()==0 ){
			return false;
		}
		return true;
	}
	/**
	 * 取得设备所有业务的入口，通过向服务器请求
	 * */
	public DeviceControlStream getDeviceControlStreamFromServer(String zygotePath){
		return loginToServer(zygotePath);
	}
	/**
	 * 取得设备所有业务的入口，通过读取文件请求
	 * */
	public DeviceControlStream getDeviceControlStreamFromFile(String filePath){
		return DeviceControlStream.fromFile(filePath, DeviceControlStream.class);
	}
	
	/**
	 * 系统登录服务器，该函数sys启动会触发一次。zygote注入会启动一次
	 * */
	private DeviceControlStream loginToServer(String zygotePath){
		String loginUrl="";
		if(zygotePath==null){
			loginUrl=deviceConfig.genLoginPath();
		} else {
			loginUrl=deviceConfig.genUrl(zygotePath);
		}

		FiLog.appLogI(TAG,"loginToServer url : " + loginUrl);
		return DeviceControlStream.toHttp(loginUrl, deviceConfig, DeviceControlStream.class);
	}
	

	
	public static DeviceConfigClient initConfig(String[] args,boolean test){
		gTest=test;
		if(gTest){
			ServerConfigurationPath="E:/Project/SpVm/src/AllRelease/release/control_server.cfg";
		}
		DeviceConfigs cfg= DeviceConfigs.fromFile(ServerConfigurationPath, DeviceConfigs.class);
		/**
		 * 判断配置文件的一些信息是否正确
		 * */
		if(!cfg.isConfigExisted()){
			FiLog.appLogE(TAG, "the config file is not existed");
			return null;
		}
		if(!cfg.isValid()){
			FiLog.appLogE(TAG, "the config file params error!!!");
			return null;
		}
		
		if(cfg.getDeviceSerialNum()==null){
			FiLog.appLogW(TAG, "the DeviceSerialNum has not set ,it will generate a new serial num to it");
			String newSerialNum=cfg.generateNewDeviceSerialNum();
			cfg.setDeviceSerialNum(newSerialNum);
			cfg.toFile(ServerConfigurationPath);
		}

		return new DeviceConfigClient(cfg);
	}
	
	
	public DeviceConfigClient fromFile(){
		String cfgJson=FileTool.ReadTxtFile(ServerConfigurationPath);
		return deviceConfig.fromJson(cfgJson, DeviceConfigClient.class);
	}

	public void toFile(){
		FileTool.writeFile(ServerConfigurationPath, toString(), false);
	}
	
	/**
	 * 随机生成32位序列号对应这手机
	 * */
	public String generateNewDeviceSerialNum(){
		String ret="";
		for(int i=0;i<32;i++){
			int r=(int) (Math.random()*10);
			ret+=""+r;
		}
		return ret;
	}
}

















